﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Sherwood.Security;
using Sherwood.SignOn.Server;
using Sherwood.SignOn.Server.Models;
using Sherwood.SignOn.Server.Models.Repositories;
using Sherwood.SignOn;
using System.Web;
using Sherwood.SignOn.Server.Models.Services;

namespace Sherwood.SignOn.Server.Models.Repositories.Sql
{
    public class UserSessionRepository : IUserSessionRepository
    {

        public IUserSession GetUserSession(Guid userSessionId)
        {
            SqlRepositoryDataContext db = new SqlRepositoryDataContext();
            IUserSession session = (from s in db.UserSessions where s.Id.Equals(userSessionId) select s).FirstOrDefault();
            db.Dispose();
            return session;
        }

        public IUserSession AddUserSession(Guid userAccountId, string ipAddress, bool persistent)
        {
            SqlRepositoryDataContext db = new SqlRepositoryDataContext();
            UserSession session = new UserSession();
            session.Id = Guid.NewGuid();
            if (userAccountId != Guid.Empty)
                session.UserAccountId = userAccountId;
            session.DateCreated = DateTime.Now;
            session.IpAddress = ipAddress;
            session.Persistent = persistent;
            db.UserSessions.InsertOnSubmit(session);
            db.SubmitChanges();
            db.Dispose();
            return session;
        }

        public void RemoveUserSession(Guid signOnSessionId)
        {
            SqlRepositoryDataContext db = new SqlRepositoryDataContext();
            UserSession session = (from s in db.UserSessions where s.Id.Equals(signOnSessionId) select s).FirstOrDefault();
            foreach (UserSessionClientRecord clientRecord in session.UserSessionClientRecords)
            {
                db.UserSessionClientRecords.DeleteOnSubmit(clientRecord);
            }
            db.UserSessions.DeleteOnSubmit(session);
            db.SubmitChanges();
            db.Dispose();
        }

        public void AddUserSessionClientRecord(Guid userSessionId, string clientSessionId, string signOutUrl)
        {
            signOutUrl = signOutUrl ?? ""; //previously "to lower"
            SqlRepositoryDataContext db = new SqlRepositoryDataContext();
            UserSession session = (from s in db.UserSessions where s.Id.Equals(userSessionId) select s).FirstOrDefault();

            if (session != null)
            {
                UserSessionClientRecord clientRecord = (from r in db.UserSessionClientRecords
                                                        where
                                                            r.ClientSessionId != null &&
                                                            r.ClientSessionId.Equals(clientSessionId) &&
                                                            r.SignOutUrl != null && r.SignOutUrl.ToLower() == signOutUrl
                                                        select r).FirstOrDefault();
                if (clientRecord == null) //if client hasn't already been recorded
                {
                    clientRecord = new UserSessionClientRecord();
                    clientRecord.UserSession = session;
                    clientRecord.ClientSessionId = clientSessionId;
                    clientRecord.SignOutUrl = signOutUrl;
                    db.UserSessionClientRecords.InsertOnSubmit(clientRecord);
                    db.SubmitChanges();
                }
            }
            db.Dispose();
        }      
    }
}
